package api

import (
	"GopherFocus/app/system/index/internal/define"
	"GopherFocus/app/system/index/internal/service"
	"GopherFocus/library/response"
	"github.com/gogf/gf/net/ghttp"
)

var File = fileApi{}

type fileApi struct{}

func (a *fileApi) Upload(r *ghttp.Request) {
	// 获取上传的表单域
	file := r.GetUploadFile("file")
	if file == nil {
		response.JsonExit(r, 1, "请选择需要上传的文件")
	}
	if data, err := service.File.Upload(r.Context(), define.FileUploadInput{
		File:       file,
		RandomName: true,
	}); err != nil {
		response.JsonExit(r, 1, err.Error())
	} else {
		response.JsonExit(r, 0, "", &define.FileUploadRes{
			Name: data.Name,
			Url:  data.Url,
		})
	}
}
